package br.com.j7.jdesk.persistence.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Version;

import br.com.j7.jdesk.persistence.listener.UserGroupListener;

/**
 * Entidade UserGroup
 * 
 * @author Edivando Alves
 *
 */
@Entity
@EntityListeners(value={UserGroupListener.class})
@NamedQueries( { @NamedQuery(name="userGroup", query="select ug from UsersGroups ug"), 
				 @NamedQuery(name="userGroupByEnterprise", query="from UsersGroups where enterprise = ?1"), 
				 @NamedQuery(name="userGroupByCdUserGroupAndEnterprise", query="Select ug from UsersGroups ug where ug.cdUserGroup = ?1 AND ug.enterprise = ?2")} )

public class UsersGroups implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer idUserGroup;
	@OneToMany(mappedBy="userGroup", cascade = CascadeType.PERSIST)
	private List<Users> user;
	@Column(nullable=true, length=80)
	private String cdUserGroup;
	@Lob
	@Column(nullable=true)
	private String dsUserGroup;
	@ManyToOne
	@JoinColumn(name="idEnterprise")
	private Enterprises enterprise;

	@OneToMany(mappedBy="userGroup", cascade=CascadeType.PERSIST)
	private List<UsersGroupsPages> usersGroupsPages;
	
	@Version
	private Integer nrVersion;
	
	public UsersGroups() {
	}
	
	public UsersGroups(Integer idUserGroup) {
		this.idUserGroup = idUserGroup;
	}
	
	public Integer getIdUserGroup() {
		return idUserGroup;
	}

	public void setIdUserGroup(Integer idUserGroup) {
		this.idUserGroup = idUserGroup;
	}

	public String getCdUserGroup() {
		return cdUserGroup;
	}

	public void setCdUserGroup(String cdUserGroup) {
		this.cdUserGroup = cdUserGroup;
	}

	public String getDsUserGroup() {
		return dsUserGroup;
	}

	public void setDsUserGroup(String dsUserGroup) {
		this.dsUserGroup = dsUserGroup;
	}

	public List<Users> getUser() {
		if(user == null){
			user = new ArrayList<>();
		}
		return user;
	}

	public UsersGroups setUser(List<Users> user) {
		this.user = user;
		return this;
	}

	public Integer getNrVersion() {
		return nrVersion;
	}
	
	public void setNrVersion(Integer nrVersion) {
		this.nrVersion = nrVersion;
	}
	
	public List<UsersGroupsPages> getUsersGroupsPages() {
		return usersGroupsPages;
	}
	
	public void setUsersGroupsPages(List<UsersGroupsPages> usersGroupsPages) {
		this.usersGroupsPages = usersGroupsPages;
	}

	public Enterprises getEnterprise() {
		return enterprise;
	}
	
	public void setEnterprise(Enterprises enterprise) {
		this.enterprise = enterprise;
	}
	
	@Override
	public String toString() {
		return cdUserGroup;
	}

	@Override
	public int hashCode() {
		return idUserGroup;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		UsersGroups other = (UsersGroups) obj;
		if (idUserGroup == null) {
			if (other.idUserGroup != null)
				return false;
		} else if (!idUserGroup.equals(other.idUserGroup))
			return false;
		return true;
	}
	
	
}
